
import logging
import time
import user as us
from dm import dm
import sthz as gb
import random
import re
from util import check_has_pl,move_click,yx_start_pos,move_click_with_xd
import init
def extract_numbers(s):
    return [int(num) if num.isdigit() else float(num) for num in re.findall(r'\d+(?:\.\d+)?', s)]
rw_sw = False
dm.SetDict(0,'./word/怪物字库.txt')
st_cs = 7
jt_cs = 0
large_kill_map={
    "2":"",
}
while True:
    large_index = dm.Ocr(270+yx_start_pos[0],494+yx_start_pos[1],554+yx_start_pos[0],564+yx_start_pos[1],"f8f800-123456",1.0)
    small_index = dm.Ocr(281+yx_start_pos[0],558+yx_start_pos[1],485+yx_start_pos[0],596+yx_start_pos[1],"f8f800-123456",1.0)
    print(large_index,small_index)
    if len(large_index)>=4 and len(small_index)>=2:
        large_kill_map["3"] = large_index[0]
        large_kill_map["6"] = large_index[0]+"|"+large_index[1]
        large_kill_map["9"] = large_index[0]+"|"+large_index[1]+"|"+large_index[2]
        large_kill_map["10"] = large_index[0]+"|"+large_index[1]+"|"+large_index[2]+"|"+large_index[3]
        large_kill_map["4"] = large_kill_map["6"]
        large_kill_map["5"] = large_kill_map["9"]
        large_kill_map["7"] = large_kill_map["4"]
        large_kill_map["8"] = large_kill_map["5"]
        large_kill_map["11"] = large_kill_map["10"]
        large_kill_map["13"] = large_kill_map["10"]
        break
    else:
        print("技能找不到请摆放好技能")
        time.sleep(1)
user_skill_list = []
kill_base_x = 311
large_kill = [81,87,69,82,84]
buff_kill = [18,17]
small_kill=[65,83,68,70,71]
largest_kill = [89,72]
large_index = 0
def press_kill(key):
    print("按技能")
    gb.presskeybyvalue(key)
    time.sleep(random.uniform(0.3, 0.4))
    gb.releasekeybyvalue(key)
    time.sleep(random.uniform(0.3, 0.4))

def check_xl():
   dm_ret = dm.SetDict(0,'./word/系统字库.txt')
   hong = dm.Ocr(234+yx_start_pos[0],547+yx_start_pos[1],273+yx_start_pos[0],581+yx_start_pos[1],"ff6600-123456",1)
   lan = dm.Ocr(525+yx_start_pos[0],549+yx_start_pos[1],568+yx_start_pos[0],577+yx_start_pos[1],"00a0e8-123456",1)
   hong = extract_numbers(hong)
   lan = extract_numbers(lan)
   if len(hong)>0:
    hong  = hong[0]
    print('红血量',hong)
    if hong > 100:
        hong = hong // 10
    if hong < 40 and hong>5:
        print('吃红')
        gb.pressandreleasekeybyvalue(49)
   if len(lan)>0:
    lan  = lan[0]
    print('蓝血量',lan)
    if lan > 100:    
        print('吃蓝')
        hong = lan // 10
    if lan < 40 and lan>5:
            gb.pressandreleasekeybyvalue(50)  
   dm_ret = dm.SetDict(0,'./word/怪物字库.txt')
def release_large_skills(key):
        global rw_sw
        i = 0
        check_xl()
        press_kill(key)
        while True:
            ret = dm.FindStrFastEx(0,0,1024,768, "A|B|C|D|E|F|G|H|I|J|K|L|M|N","f8f800-123456",1).split("|")
            if len(ret)>2:
                print("技能释放完")
                us.room_id = us.user_in_hbl_which_map()
                break
            else:
                if judge_user_sw():
                    print('人物死亡')
                    rw_sw = True
                    return True
                if i<random.randint(4,9):
                    gb.presskeybyvalue(38)
                    time.sleep(random.uniform(0.2, 0.5))
                    gb.releasekeybyvalue(38)
                    i+=1
                print("正在放技能")
                if us.room_id == 11:
                     zctz = dm.FindPic(0,0,1024,768,"./nyz_dt/boss.bmp","123456",1,2)
                     if zctz[0]==-1:
                        print('boss死了')
                        return True
                time.sleep(random.uniform(0.5, 0.8))


def shouji_jn():
    if rw_sw:
        return []
    global small_index
    jn_base_x = 311
    jn_str = large_kill_map[str(us.room_id)]+"|"+"|".join(small_index)
    jn_list = []
    while True:
        ret = dm.FindStrFastEx(0,0,1024,768, jn_str,"f8f800-123456",1).split("|")
        if ret[0]:
            break
        else:
            print("收集技能失败,人物向上移动")
            gb.presskeybyvalue(38)
            time.sleep(1)
            gb.releasekeybyvalue(38)
            time.sleep(1)
    for i in ret:
        i=i.split(",")
        kill_i = (int(i[1]) - yx_start_pos[0]-jn_base_x)//30
        if kill_i>=len(small_index) or kill_i>=len(large_kill):
                continue
        if (int(i[2]) - yx_start_pos[1])-560>0:
            jn_list.append(small_kill[kill_i])
        else:
            jn_list.append(large_kill[kill_i])
    random.shuffle(jn_list)
    jn_list.append(-1)
    return jn_list
cur_room_id = -1

def judge_user_sw():
    global rw_sw
    rwsw = dm.FindPic(0,0,1024,768,"./nyz_dt/rwsw.bmp","123456",0.9,0)
    if rwsw[0]!=-1:
        rw_sw = True
        return True
    hong = dm.Ocr(234+yx_start_pos[0],547+yx_start_pos[1],273+yx_start_pos[0],581+yx_start_pos[1],"ff6600-123456",1)
    lan = dm.Ocr(525+yx_start_pos[0],549+yx_start_pos[1],568+yx_start_pos[0],577+yx_start_pos[1],"00a0e8-123456",1)
    hong = extract_numbers(hong)
    lan = extract_numbers(lan)
    if len(hong)>0:
        hong  = hong[0]
    if len(lan)>0:
        lan  = lan[0]
    if hong==0 or lan==0:
        rw_sw = True
        return True
    return False
   

def user_zd():
        global rw_sw
        jn_list = []
        ret = dm.FindStrFast(0,0,1024,768,"開門","f8ff49-123456",1.0)
        print(ret,'门')
        if ret[0]!=-1:
            # 有门退出 
            print('有门')
            return
        if us.room_id == 14:
            rw_sw = True
            return 
        if us.room_id==2:
            us.move_to_target(43,50)
            press_kill(39)
            release_large_skills(81)
        elif us.room_id==3:
            us.move_to_target(38,55)
            press_kill(39)
            release_large_skills(87)
        elif us.room_id==4:
            us.move_to_target(63,52)
            press_kill(39)
            release_large_skills(69) 
        elif us.room_id==5:
            us.move_to_target(30,56)
            press_kill(39)
            release_large_skills(82) 
        elif us.room_id==6:
            us.move_to_target(36,48)
            press_kill(39)
            release_large_skills(69) 
        elif us.room_id==7:
            us.move_to_target(40,51)
            press_kill(39)
            release_large_skills(69) 
        elif us.room_id==8:
            us.move_to_target(52,52)
            press_kill(39)
            release_large_skills(82) 
        elif us.room_id==9:
            us.move_to_target(62,50)
            press_kill(39)
            release_large_skills(82)
        elif us.room_id==13:
            us.move_to_target(30,49)
            press_kill(39)
            release_large_skills(84)
        elif us.room_id==10:
            us.move_to_target(30,49)
            press_kill(39)
            release_large_skills(84) 
        elif us.room_id==11:
            press_kill(39)
            res = release_large_skills(89)
            if not res:
                res = release_large_skills(72)
                if res:
                    return
            else:
                return
        elif us.room_id==-1 or us.room_id==14:
            rw_sw = True
            return   
        
        if us.check_is_toogle_room():
            user_zd()
            return
        jn_list = shouji_jn()
        key = 0
        local = 0
        pos_list=[[(180,600),(360,600),(985,600)],
                  [(235,600),(470,600),(868,600)],
                  [(226,600),(452,600),(878,600)],
                  [(250,730),(579,732),(851,732)],
                  [(220,619),(446,619),(868,619)],
                  [(300,730),(598,730),(459,730)],
                  [(225,610),(452,610),(646,610)],
                  [(225,610),(452,610),(882,610)],
                  [(230,595),(464,595),(864,595)]]
        if rw_sw:
            return
        # while循环技能数组 
        while key<len(jn_list):
            us.room_id = us.user_in_hbl_which_map()
            if us.room_id == -1 or us.room_id == 14:
                rw_sw = True
                return
            if judge_user_sw():
                return
            if us.room_id!=11 and us.room_id!=14:
                if us.room_id==13:
                    cur_poss = pos_list[us.room_id-5][local]
                else:
                    cur_poss = pos_list[us.room_id-2][local]
                # 找怪 
                for i in range(5):
                    gw = dm.FindPic(0,0,1024,768,"./nyz_dt/gw.bmp|./nyz_dt/gw2.bmp|./nyz_dt/gw3.bmp|./nyz_dt/gw5.bmp|./nyz_dt/gw7.bmp|./nyz_dt/gw8.bmp|./nyz_dt/gw9.bmp","123456",0.9,0)
                    if gw[0]!=-1:
                        break
                # 有怪 移动怪旁边 放技能 循环技能数组
                if gw[0]!=-1:
                    ret = dm.FindStrFast(0,0,1024,768,"開門","f8ff49-123456",1.0)
                    if ret[0]!=-1:
                        print("有门退出")
                        # 有门退出 
                        return
                    x,y = us.user_hbl_real_pos(gw[1],gw[2]+65)
                    us.get_real_pos()
                    logging.error(f'gw pos x:{x} y:{y}')
                    logging.error(f'user pos x:{us.global_real_x} y:{us.global_real_y}')
                    print('user pos',us.global_real_x,us.global_real_y)
                    print('user pos',us.global_real_x,us.global_real_y)
                    # 判断怪物在左侧还是右侧

                    if x<10:
                        us.move_to_target(x+10,y)
                        press_kill(37)
                    elif x>100:
                        us.move_to_target(x-10,y)
                        press_kill(39)
                    else:
                        if us.global_get_pos>x:
                            us.move_to_target(x+145,y)
                            press_kill(37)
                        else:
                            us.move_to_target(x-10,y)
                            press_kill(39)
                    release_large_skills(jn_list[key])
                    key+=1
                    if key>=len(jn_list):
                        return
                else:
                    print("没怪")
                    # 无怪 找门
                    ret = dm.FindStrFast(0,0,1024,768,"開門","f8ff49-123456",1.0)
                    if ret[0]!=-1:
                        print("有门退出")
                        # 有门退出 
                        return
                    else:
                        if us.room_id==11:
                            continue
                        # 无门 循环位置数组 移动到第一位置位置指针++ 继续循环技能数组
                        print('没有门移动')
                        if judge_user_sw():
                            return
                        us.move_to_target(cur_poss[0]//10,cur_poss[1]//10)
                        local+=1
                        if local>2:
                            if us.room_id == 13:
                                us.room_id == 11
                                return
                            local = 0
            else:
                boss = dm.FindPic(0,0,1024,768,"./nyz_dt/boss.bmp","123456",1,2)
                if boss[0]!=-1:
                    release_large_skills(jn_list[key])
                    key+=1
                    if key>=len(jn_list):
                        return
                else:
                    return
def check_is_ds():
    global jt_cs
    print(jt_cs,'检查倒数')
    jt_cs +=1
    if jt_cs>=10:
         gb.presskeybyvalue(40)
         time.sleep(0.5)
         gb.releasekeybyvalue(40)
         gb.presskeybyvalue(37)
         time.sleep(0.5)
         gb.releasekeybyvalue(37)
         jt_cs=0
    ds = dm.FindPic(0,0,1024,768,"./nyz_dt/ds1.bmp|./nyz_dt/ds2.bmp|./nyz_dt/ds3.bmp","123456",0.9,0)
    print(ds)
    if ds[0]!=-1:
        time.sleep(3)
        print("未检装备")

def pick_up():
     if cur_room_id == us.room_id:
        while True:
            wp = dm.FindPic(136+yx_start_pos[0],0,1024,768,"./nyz_dt/wpy.bmp","123456",0.9,0)
            if wp[0]!=-1:
                x,y = us.user_hbl_real_pos(int(wp[1])-40,int(wp[2])+40)
                print('捡东西移动')
                us.move_to_target(x,y)
                gb.pressandreleasekeybyvalue(88)
            else:
                break

def is_under_esc():
    press_kill(27)
    esc = dm.FindPic(0,0,1024,768,"./nyz_dt/esc.bmp","123456",1,2)
    if esc[0]!=-1:
        press_kill(27)

def ss_fj():
    is_under_esc()
    # 修理分解机
    while True:
        xlfjj = dm.FindPic(0,0,1024,768,"./nyz_dt/xlfhjj.bmp","123456",1,0)
        if xlfjj[0]!=-1:
            move_click(xlfjj[1]+50,xlfjj[2]+10)
            time.sleep(random.randrange(1,2))
            gb.movemouseto(xlfjj[1]+100,xlfjj[2]+100)
            time.sleep(random.randrange(1,2))
            break
        else:
            press_kill(48)
            time.sleep(random.randrange(1,2))
    while True:
        zbfj = dm.FindPic(0,0,1024,768,"./nyz_dt/zbfj.bmp","123456",1,0)
        if zbfj[0]!=-1:
            move_click(zbfj[1]+10,zbfj[2]+5)
            time.sleep(random.randrange(2,4))
            press_kill(65)
            time.sleep(random.randrange(1,2))
            press_kill(32)
            time.sleep(random.randrange(2,4))
            press_kill(32)
            time.sleep(random.randrange(2,4))
            qr = dm.FindPic(0,0,1024,768,"./nyz_dt/qr.bmp","123456",0.9,0)
            if qr[0]!=-1:
                move_click(qr[1]+10,qr[2]+5)
                time.sleep(random.randrange(1,2))
                gb.movemouseto(qr[1]+100,qr[2]+100)
                time.sleep(random.randrange(1,2))
            ss = dm.FindPicEx(0,0,1024,768,"./nyz_dt/sss.bmp|./nyz_dt/sss2.bmp|./nyz_dt/sss3.bmp","123456",0.9,0)
            if not ss:
                press_kill(27)
                return 
            ss = ss.split('|')
            for s in ss:
                s = s.split(',')
                move_click(int(s[1])+15,int(s[2])+15)
                time.sleep(random.uniform(0.3,0.7))
            time.sleep(random.randrange(1,2))
            gb.movemouseto(100,100)
            fj = dm.FindPic(0,0,1024,768,"./nyz_dt/fj.bmp","123456",0.9,0)
            if fj[0]!=-1:
                move_click(int(fj[1])+27,int(fj[2])+11)
                time.sleep(random.uniform(0.3,0.7))
            else:
                print('找不到分解')
            qr = dm.FindPic(0,0,1024,768,"./nyz_dt/qr.bmp","123456",0.9,0)
            if qr[0]!=-1:
                    move_click(qr[1]+10,qr[2]+5)
                    time.sleep(random.uniform(0.3,0.7))
            time.sleep(random.randrange(4,6))
            is_under_esc()
            break
        else:
            qr = dm.FindPic(0,0,1024,768,"./nyz_dt/qr.bmp","123456",0.9,0)
            if qr[0]!=-1:
                move_click(qr[1]+10,qr[2]+5)
                time.sleep(random.randrange(1,2))
                gb.movemouseto(qr[1]+100,qr[2]+100)
                time.sleep(random.randrange(1,2))
def start_toogle():
    try:
        global cur_room_id
        global st_cs
        global jt_cs
        global rw_sw
        pick = 0
        dm_ret = dm.SetDict(0,'./word/怪物字库.txt')
        add_buff=False
        while True:
            us.global_get_pos=True
            us.room_id = us.user_in_hbl_which_map()
            print("当前地图",us.room_id)
            if cur_room_id!=us.room_id:
                pick=0
                cur_room_id = us.room_id
            if us.room_id!=1:
                user_zd()
                us.room_id = us.user_in_hbl_which_map()
           
            if us.room_id == 1:
                for i in range(5):
                    zx = dm.FindPic(0,0,1024,768,"./nyz_dt/zx.bmp","123456",1,0)
                    if zx[0]!=-1:
                        move_click_with_xd(782,199,yx_start_pos)
                        break
                if not add_buff:
                    for i in buff_kill:
                        press_kill(i)
                    add_buff = True
                us.move_to_target(62,34)
                jt_cs +=1
            elif us.room_id == 2:
                if pick==0:
                    pick_up()
                us.move_to_target(126,52,isRun=True)
                check_is_ds()
            elif us.room_id == 3:
                if pick==0:
                    pick_up()
                us.move_to_target(60,41,isRun=True)
                check_is_ds()
            elif us.room_id == 4:
                if pick==0:
                    pick_up()
                us.move_to_target(84,43,isRun=True)
                check_is_ds()
            elif us.room_id == 5:
                if pick==0:
                    pick_up()
                us.move_to_target(104,39,isRun=True)
                check_is_ds()
            elif us.room_id == 6:
                if pick==0:
                    pick_up()
                us.move_to_target(60,41,isRun=True)
                check_is_ds()
            elif us.room_id == 7:
                if pick==0:
                    pick_up()
                us.move_to_target(107,52,isRun=True)
                check_is_ds()
            elif us.room_id == 8:
                if pick==0:
                    pick_up()
                us.move_to_target(101,51,isRun=True)
                check_is_ds()
            elif us.room_id == 9:
                if pick==0:
                    pick_up()
                us.move_to_target(125,50,isRun=True)
                check_is_ds()
            elif us.room_id == 10 or us.room_id==13:
                add_buff=False
                if pick==0 and us.room_id!=13:
                    pick_up()
                us.move_to_target(126,50,isRun=True)
                if us.room_id==13:
                    time.sleep(3)
                check_is_ds()
            elif us.room_id == 11:
                under_esc = False
                while True:
                    if judge_user_sw():
                        return
                    print("在11房间检测")
                    ret = check_has_pl(dm)
                    if not ret:
                        print("人物没有疲劳了")
                        time.sleep(7)
                        press_kill(27)
                        time.sleep(3)
                        press_kill(57)
                        time.sleep(2)
                        for i in range(3):
                             press_kill(88)
                        ss_fj()
                        for i in range(random.randrange(3,8)):
                            press_kill(123)
                        time.sleep(random.randrange(5,8))
                        return 'wpl'
                    zctz = dm.FindPic(0,0,1024,768,"./nyz_dt/zctz.bmp","123456",1,0)
                    print(zctz,'无再次挑战')
                    if zctz[0]!=-1:
                        esc = dm.FindPic(0,0,1024,768,"./nyz_dt/esc.bmp","123456",0.9,2)
                        if esc[0]!=-1:
                            press_kill(27)
                        press_kill(57)
                        for i in range(3):
                             press_kill(88)
                        time.sleep(random.randrange(2,5))
                        if st_cs%7 == 0:
                            press_kill(65)
                            time.sleep(random.randrange(2,5))
                            press_kill(32)
                            time.sleep(random.randrange(1,2))
                            press_kill(37)
                            time.sleep(random.randrange(1,3))
                            press_kill(32)
                            ss_fj()
                        if st_cs%12 == 0:
                            #差个分解ss
                            ss_fj()
                        if st_cs%12!=0 and st_cs%7!=0:
                            press_kill(27)
                        for i in range(random.randrange(3,8)):
                            press_kill(121)
                        time.sleep(3)   
                        us.room_id=1
                        st_cs+=1
                        break
                    else:
                        boss = dm.FindPic(0,0,1024,768,"./nyz_dt/boss.bmp","123456",1,2)
                        if boss[0]!=-1:
                            break
                        time.sleep(7)
                        if not under_esc:
                            press_kill(27)
                        under_esc = True
            elif us.room_id == 14:
                while True:
                    print('误入裂缝返回城镇')
                    fhcz =  dm.FindPic(0,0,1024,768,"./nyz_dt/fhcz.bmp","123456",1,2)
                    if fhcz[0]!=-1:
                        move_click(int(fhcz[1]),int(fhcz[2]))
                        time.sleep(1)
                        qr = dm.FindPic(0,0,1024,768,"./nyz_dt/qr.bmp","123456",0.9,0)
                        move_click(qr[1],qr[2])
                        time.sleep(1)
                        rw_sw = True
                        return
                    else:
                        press_kill(27)
            elif us.room_id==-1:
                rw_sw = True
                return
            if rw_sw:
                return
            pick+=1
    except Exception as ex:
         print("出现如下异常%s"%ex)
         gb.presskeybyvalue(40)
         time.sleep(0.5)
         gb.releasekeybyvalue(40)
         gb.presskeybyvalue(37)
         time.sleep(0.5)
         gb.releasekeybyvalue(37)
         start_toogle()

def before_start():
    global rw_sw
    boss = dm.FindPic(0,0,1024,768,'./nyz_dt/boss.bmp',"123456",1,2)
    if boss[0]==-1:
        init.go_hbl()
    gb.movemouseto(1024,768)
   
    time.sleep(1)
    try:
        start_toogle()
    except Exception:
        start_toogle()
    while True:
        hbl_mk  = dm.FindPic(0,0,1024,768,"./nyz_dt/hbl_mk.bmp|./nyz_dt/hbl_mk2.bmp|./nyz_dt/hbl_mk3.bmp","123456",1,0)
        if hbl_mk[0]!=-1:
            break
        else:
            print('人物死亡或无疲劳,海博伦门口,人物死亡:',rw_sw)
    if rw_sw:
        while True:
            xr = dm.FindPic(0,0,1024,768,"./nyz_dt/rwxr.bmp","123456",0.9,0)
            if xr[0]!=1:
                print('人物虚弱了')
                break
            else:
                print('没有找到人物虚弱')
        # 点击确认
        qr = dm.FindPic(0,0,1024,768,"./nyz_dt/qr.bmp","123456",0.9,0)
        if qr[0]!=-1:
            move_click(qr[1],qr[2],yx_start_pos)
            time.sleep(random.randrange(1,3))
            qyhf = dm.FindPic(0,0,1024,768,"./nyz_dt/qyhf.bmp","123456",0.9,0)
            if qyhf[0]!=-1:
                move_click(qyhf[1],qyhf[2],yx_start_pos)
                time.sleep(random.randrange(1,3))
            else:
                print('没有找到契约回复按钮')
        else:
            print('没有找到虚弱的确认按钮')
        press_kill(37)
        rw_sw = False
    init.go_hbl()